<?php !defined('BASE_ROOT') && exit ( 'Access Denied!' );
class Mysql {

	protected $querynum = 0;
	protected $connstring;

	public function __construct($dbhost,$dbusername,$dbpassword,$dbname = '',$charset = 'utf8',$pconnect = 0,$new_link = false){
		if($pconnect) {
			if(!$this->connstring = mysql_pconnect($dbhost, $dbusername, $dbpassword,$new_link)) {
				show_error('无法连接到数据库!');
			}
		} else {
			if(!$this->connstring = mysql_connect($dbhost, $dbusername, $dbpassword,$new_link)) {
				show_error('无法连接到数据库!');
			}
		}
		mysql_select_db($dbname) or die (show_error('数据库选择失败!'));
		$charset = str_replace('-', '', $charset);
		mysql_query("SET NAMES '$charset'");
		return $this->connstring;
	}

	public function query($sql, $type = '', $cachetime = FALSE) {
		//$sql = mysql_real_escape_string($sql);
		$func = $type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
		if(!($result = $func($sql, $this->connstring)) && $type != 'SILENT') {
			show_error('查询失败！<br/>'.$this->getError().'<br/>SQL:'.$sql);
		}
		$this->querynum++;
		return $result;
	}

	public function fetch_array($result,$result_type=MYSQL_ASSOC){
		return mysql_fetch_array($result, $result_type);
	}

	public function fetch_assoc($result){
		return mysql_fetch_assoc($result);
	}

	public function fetch_row($result) {     
		return mysql_fetch_row($result);  
	}

	public function num_fields($result){
		return mysql_num_fields($result);
	}

	public function num_rows($result){
		return mysql_num_rows($result);
	}

	public function insert_id() {
		return ($id = mysql_insert_id($this->connstring)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}

	public function affected_rows() {//取得所影响的行数  
		return mysql_affected_rows($this->connstring);  
	}

	public function result($query, $row = 0) {
		return @mysql_result($query, $row);
	}

	public function rows_count($sql){
		$result = $this->query($sql);
		$rowsnum = $this->num_rows($result);
		$this->querynum++;
		return $rowsnum;
	}

	public function free_result($result){
		return mysql_free_result($result);
	}

	public function getOne($sql) {
		$result = $this->query($sql);
		$row = $this->fetch_assoc($result);
		$this->querynum++;
		$this->free_result($result);
		return array_shift($row);
	}

	public function getRow($sql){
		$result = $this->query($sql);
		$data = $this->fetch_assoc($result);
		$this->querynum++;
		$this->free_result($result);
		return $data;
	}

	public function getAll($sql){
		$arr = array();
		$result = $this->query($sql);
		while($data = $this->fetch_array($result)) {
			$arr[] = $data;
		}
		$this->querynum++;
		$this->free_result($result);
		return $arr;
	}

	//添加数据
	public function insert($tablename, $insertsqlarr, $returnid=0, $replace = false, $silent=0) {
		$insertkeysql = $insertvaluesql = $comma = '';
		foreach ($insertsqlarr as $insert_key => $insert_value) {
			$insertkeysql .= $comma.'`'.$insert_key.'`';
			$insertvaluesql .= $comma.'\''.$insert_value.'\'';
			$comma = ', ';
		}
		$method = $replace?'REPLACE':'INSERT';
		$this->query($method.' INTO '.$tablename.' ('.$insertkeysql.') VALUES ('.$insertvaluesql.')', $silent?'SILENT':'');
		if($returnid && !$replace) {
			return $this->insert_id();
		}
	}

	//更新数据
	public function update($tablename, $setsqlarr, $wheresqlarr, $silent=0) {
		$setsql = $comma = '';
		foreach ($setsqlarr as $set_key => $set_value) {
			$setsql .= $comma.'`'.$set_key.'`'.'=\''.$set_value.'\'';
			$comma = ', ';
		}
		$where = $comma = '';
		if(empty($wheresqlarr)) {
			$where = '1';
		} elseif(is_array($wheresqlarr)) {
			foreach ($wheresqlarr as $key => $value) {
				$where .= $comma.'`'.$key.'`'.'=\''.$value.'\'';
				$comma = ' AND ';
			}
		} else {
			$where = $wheresqlarr;
		}
		$this->query('UPDATE '.$tablename.' SET '.$setsql.' WHERE '.$where, $silent?'SILENT':'');
	}
	//删除数据
	public function delete($tablename, $var='id', $range) {
		if($range) {
			if(is_array($range)){
				$range = implode(',',$id);
			}
			$this->query('DELETE FROM '.$tablename.' WHERE '.$var.' in ('.$range.')');
		}
	}

	public function close(){
		if(isset($this->connstring)){
			mysql_close($this->connstring);
		}
	}

	public function getversion(){
		return mysql_get_server_info($this->connstring);
	}

	public function getQueryNum() {
		return $this->querynum;
	}

	public function getError() {
		return mysql_errno() . " : " . mysql_error();
	}
}